{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import gym\n",
    "from termcolor import colored"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "==============================\n",
      "Player:20, ace:False, Dealer:18\n",
      "Player Simple strategy take action:STAND\n",
      "Player:20, ace:False, Dealer:18\n",
      "Game win.(Reward 1)\n",
      "PLAYER:\u001b[31m[10, 10]\u001b[0m\t DEALER:\u001b[32m[8, 10]\u001b[0m\n",
      "\n",
      "==============================\n",
      "Player:21, ace:True, Dealer:14\n",
      "Player Simple strategy take action:STAND\n",
      "Player:21, ace:True, Dealer:24\n",
      "Game win.(Reward 1)\n",
      "PLAYER:\u001b[31m[1, 10]\u001b[0m\t DEALER:\u001b[32m[10, 4, 10]\u001b[0m\n",
      "\n",
      "==============================\n",
      "Player:17, ace:True, Dealer:20\n",
      "Player Simple strategy take action:HIT\n",
      "Player:17, ace:False, Dealer:20\n",
      "Player Simple strategy take action:HIT\n",
      "Player:27, ace:False, Dealer:20\n",
      "Game loss.(Reward -1)\n",
      "PLAYER:\u001b[31m[6, 1, 10, 10]\u001b[0m\t DEALER:\u001b[32m[10, 10]\u001b[0m\n",
      "\n",
      "==============================\n",
      "Player:5, ace:False, Dealer:14\n",
      "Player Simple strategy take action:HIT\n",
      "Player:13, ace:False, Dealer:14\n",
      "Player Simple strategy take action:HIT\n",
      "Player:16, ace:False, Dealer:14\n",
      "Player Simple strategy take action:HIT\n",
      "Player:26, ace:False, Dealer:14\n",
      "Game loss.(Reward -1)\n",
      "PLAYER:\u001b[31m[3, 2, 8, 3, 10]\u001b[0m\t DEALER:\u001b[32m[10, 4]\u001b[0m\n",
      "\n",
      "==============================\n",
      "Player:21, ace:True, Dealer:20\n",
      "Player Simple strategy take action:STAND\n",
      "Player:21, ace:True, Dealer:20\n",
      "Game win.(Reward 1)\n",
      "PLAYER:\u001b[31m[10, 1]\u001b[0m\t DEALER:\u001b[32m[10, 10]\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "env = gym.make(\"Blackjack-v0\")\n",
    "\n",
    "def show_state(state):\n",
    "    player, dealer, ace = state\n",
    "    dealer = sum(env.dealer)\n",
    "    print(\"Player:{}, ace:{}, Dealer:{}\".format(player, ace, dealer))\n",
    "\n",
    "def simple_strategy(state):\n",
    "    player, dealer, ace = state\n",
    "    return 0 if player >= 18 else 1\n",
    "\n",
    "def episode(num_episodes):\n",
    "    episode = []\n",
    "    for i_episode in range(5):\n",
    "        print(\"\\n\" + \"=\"* 30)\n",
    "        state = env.reset()\n",
    "        for t in range(10):\n",
    "            show_state(state)\n",
    "            action = simple_strategy(state)\n",
    "            action_ = [\"STAND\", \"HIT\"][action]\n",
    "            print(\"Player Simple strategy take action:{}\".format(action_))\n",
    "            \n",
    "            next_state, reward, done, _ = env.step(action)\n",
    "            episode.append((state, action, reward))\n",
    "            if done:\n",
    "                show_state(state)\n",
    "                # [-1(loss), -(push), 1(win)]\n",
    "                reward_ = [\"loss\", \"push\", \"win\"][int(reward+1)]\n",
    "                print(\"Game {}.(Reward {})\".format(reward_, int(reward)))\n",
    "                print(\"PLAYER:{}\\t DEALER:{}\".format(colored(env.player, 'red'), \n",
    "                                                     colored(env.dealer, 'green')))\n",
    "                break\n",
    "                \n",
    "            state = next_state\n",
    "\n",
    "episode(1000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
